const { ccclass, property } = cc._decorator;

window['scale'] = 1;
@ccclass
export default class physicsIce extends cc.Component {
    private contacts: Set<string> = new Set();

    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact: cc.PhysicsContact, selfCollider, otherCollider: cc.Collider) {
        // 禁用这次碰撞
        if (otherCollider.node.name === 'physicsWater') {
            contact.disabled = true;
        }
    }

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact(contact, selfCollider, otherCollider) {
        
    }

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve(contact, selfCollider, otherCollider) {
    }

    // 每次处理完碰撞体接触逻辑时被调用
    onPostSolve(contact, selfCollider, otherCollider) {
    }

    update(dt) {
        const p1 = this.node.convertToWorldSpaceAR(cc.Vec2.ZERO);
        const p2 = cc.v2(p1.x, p1.y + this.node.height);
        const results = cc.director.getPhysicsManager().rayCast(p1, p2, cc.RayCastType.Any);

        let len = 0;
        results.forEach((result) => {
            if (result.collider.node.name !== 'physicsWater') return;
            len = result.collider.node.y - this.node.y;
        })

        // const len = results.length - 1;
        if (!len) return;
        const body = this.getComponent(cc.RigidBody);
        body.applyForceToCenter(cc.v2(0, len * window['scale']), true);
    }
}
